package cn.lcvc.xnc.authority.web.controller.backstage.authority;


import cn.lcvc.xnc.authority.model.SysPermission;
import cn.lcvc.xnc.authority.model.query.SysPermisssionQuery;
import cn.lcvc.xnc.authority.service.SysPermissionService;
import cn.lcvc.xnc.authority.web.controller.backstage.BaseBackstageController;
import cn.lcvc.xnc.application.common.model.result.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "后台权限管理模块")
@RestController
@RequestMapping(value = "/api/backstage/authority/permission")
public class SysPermissionController  extends BaseBackstageController<SysPermission, SysPermisssionQuery,Long> {

    @Autowired
    private SysPermissionService baseService;

    @ApiOperation(value = "所有权限列表")
    @PreAuthorize("hasAuthority('sys:permission:query')")
    @GetMapping("/all")
    @Override
    public ResponseData query(SysPermisssionQuery sysPermisssionQuery){
        return super.query(sysPermisssionQuery);
    }

    @ApiOperation(value = "添加权限")
    @PreAuthorize("hasAuthority('sys:permission:add')")
    @Override
    protected ResponseData add(@Valid @RequestBody SysPermission sysPermission) {
        return super.add(sysPermission);
    }


    @ApiOperation(value = "读取指定权限信息")
    @PreAuthorize("hasAuthority('sys:permission:query')")
    @Override
    protected ResponseData get(@PathVariable Long id) {
        return super.get(id);
    }

    @ApiOperation(value = "编辑权限")
    @PreAuthorize("hasAuthority('sys:permission:edit')")
    @Override
    protected ResponseData edit(@Valid @RequestBody SysPermission sysPermission) {
        return super.edit(sysPermission);
    }

    @ApiOperation(value = "批量删除权限")
    @PreAuthorize("hasAuthority('sys:permission:del')")
    @Override
    protected ResponseData deletes(Long[] ids) {
       return super.deletes(ids);
    }

    //=========================扩展部分=============================================//

    @ApiOperation(value = "所有树形权限列表")
    @GetMapping("/tree")
    @PreAuthorize("hasAuthority('sys:permission:query')")
    public ResponseData tree(SysPermisssionQuery sysPermisssionQuery){
        List<SysPermission> sysPermissions = baseService.findTree(sysPermisssionQuery);
        return ResponseData.success(sysPermissions);
    }
}
